package me.ingxin.android.keyboard.view

import android.content.Context
import android.util.AttributeSet
import me.ingxin.android.keyboard.entry.Key

/**
 * Created by ingxin on 2023/2/6
 * 字母键盘数字部分
 */
open class NumberKeyboardView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : BaseKeyboardView(context, attrs) {

    override fun onCreateKeyboard(suggestKeyWidth: Float, suggestKeyHeight: Float): List<Key> {
        val keys = ArrayList<Key>()

        var startX = paddingStart.toFloat()
        for (i in 1..9) {
            val rowY = when {
                i <= 3 -> paddingTop.toFloat()
                i <= 6 -> paddingTop + suggestKeyHeight + verticalGap
                //6<i<=9
                else -> paddingTop + 2 * suggestKeyHeight + 2 * verticalGap
            }

            if (i % 3 == 1) {
                startX = paddingStart.toFloat()
            }

            val num = '0' + i
            keys.add(
                Key(
                    startX, rowY, startX + suggestKeyWidth, rowY + suggestKeyHeight,
                    num.code, num.toString()
                )
            )
            startX += suggestKeyWidth + horizontalGap
        }

        val row4Y = paddingTop + 3 * suggestKeyHeight + 3 * verticalGap
        var row4X = paddingStart.toFloat()

        //.
        val dot = '.'
        keys.add(
            Key(
                row4X, row4Y, row4X + suggestKeyWidth, row4Y + suggestKeyHeight,
                dot.code, dot.toString()
            )
        )
        row4X += suggestKeyWidth + horizontalGap

        //0
        keys.add(
            Key(
                row4X, row4Y, row4X + suggestKeyWidth, row4Y + suggestKeyHeight,
                '0'.code, "0"
            )
        )
        row4X += suggestKeyWidth + horizontalGap

        //delete
        keys.add(
            Key(
                row4X, row4Y, row4X + suggestKeyWidth, row4Y + suggestKeyHeight,
                Key.CODE_DELETE, "delete"
            )
        )

        return keys
    }

    override fun getMaxRowSize(): Int = 4

    override fun getMaxColumnSize(): Int = 3

}